package shop.up.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import net.bytebuddy.asm.Advice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import shop.up.model.user.UserInfo;
import shop.up.user.mapper.UserInfoMapper;
import shop.up.user.service.UserService;

/**
 * 用户认证服务实现 {@link UserServiceImpl}
 *
 * @author Kevin
 * @date 2023/2/24
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    /**
     * 登录认证
     * @param userInfo
     * @return
     */
    @Override
    public UserInfo login(UserInfo userInfo) {
        // select * from user_info where login_name = ? and passwd = ?
        // 注意密码是加密：从前端获取的密码是明文
        String passwd = userInfo.getPasswd();  // 123
        // 将 passwd 进行加密 ：MD5
        String newPasswd = DigestUtils.md5DigestAsHex(passwd.getBytes());

        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("login_name", userInfo.getLoginName());
        queryWrapper.eq("passwd", newPasswd);

        UserInfo info = userInfoMapper.selectOne(queryWrapper);
        if (info != null) {
            return info;
        }
        return null;
    }
}
